package cn.wikiflyer.beikang.act.cart;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import com.unionpay.UPPayAssistEx;
import com.unionpay.uppay.PayActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;


public class UPPay {
	public static final String LOG_TAG = "PayDemo";
	private Context mContext;
	private Activity mActivity;
	private ProgressDialog mLoadingDialog = null;
    private Handler mHandler = null;
    /*****************************************************************
     * mMode参数解释： "00" - 启动银联正式环境 "01" - 连接银联测试环境
     *****************************************************************/
    private final String mMode = "00";
	
	private static final String TN_URL_01 = "http://202.101.25.178:8080/sim/gettn";	//后台网站返回订单号的地址(宋健返回，同时请求银联平台，类似于支付宝)
	
	public UPPay(Activity activity) {
		super();
		this.mContext = activity;
		this.mActivity = activity;
		 mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
		        if (mLoadingDialog.isShowing()) {
		            mLoadingDialog.dismiss();
		        }
		        String tn = "";
		        if (msg.obj == null || ((String) msg.obj).length() == 0) {
		            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		            builder.setTitle("错误提示");
		            builder.setMessage("网络连接失败,请重试!");
		            builder.setNegativeButton("确定",
		                    new DialogInterface.OnClickListener() {
		                        @Override
		                        public void onClick(DialogInterface dialog, int which) {
		                            dialog.dismiss();
		                        }
		                    });
		            builder.create().show();
		        } else {
		            tn = (String) msg.obj;
		            /*************************************************
		             * 步骤2：通过银联工具类启动支付插件
		             ************************************************/
		            doStartUnionPayPlugin(mActivity, tn, mMode);
		        }
		    }
		 };
	}
	public void doStartUnionPayPlugin(Activity activity, String tn,
            String mode){
		UPPayAssistEx.startPayByJAR(activity, PayActivity.class, null, null,
                tn, mode);
	}
	
	/** @param tnStr  传入的直接是从后台获取的订单tn'号
	 */
	public void pay(String tnStr){
        doStartUnionPayPlugin(mActivity, tnStr, mMode);
    }
	/** @param tnStr  传入的直接是从后台获取的订单tn'号
	 */
	public void payTest(String tnStr){
		mLoadingDialog = ProgressDialog.show(mContext, // context
				"", // title
				"正在努力的获取tn中,请稍候...", // message
				true); // 进度是否是不确定的，这只和创建进度条有关
		
		/*************************************************
		 * 步骤1：从网络开始,获取交易流水号即TN
		 ************************************************/
		new PayThread().start();
	}

    class PayThread extends Thread{
		@Override
		public void run() {
	        String tn = null;
	        InputStream is;
	        try {
	            String url = TN_URL_01;
	            URL myURL = new URL(url);
	            URLConnection ucon = myURL.openConnection();
	            ucon.setConnectTimeout(120000);
	            is = ucon.getInputStream();
	            int i = -1;
	            ByteArrayOutputStream baos = new ByteArrayOutputStream();
	            while ((i = is.read()) != -1) {
	                baos.write(i);
	            }
	            tn = baos.toString();
	            is.close();
	            baos.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        Message msg = mHandler.obtainMessage();
	        msg.obj = tn;
	        Log.e("wj", "tb:"+tn);
	        mHandler.sendMessage(msg);
	    }
    	
    }
    

}
